#pragma once
#include <iostream>

namespace MyRALL
{
	template <class T>
	class Shared_Ptr
	{
	public:
		Shared_Ptr(T* t)
		{ 
			_ptr = t;
			_pcount = new int(1);
		}
		Shared_Ptr(const Shared_Ptr<T>& sptr)
		{
			std::cout << "Shared_Ptr(const Shared_Ptr& sptr)" << std::endl;
			_ptr = sptr._ptr;
			_pcount = sptr._pcount;
			++(*_pcount);
		}
		Shared_Ptr<T>& operator=(const Shared_Ptr<T>& sptr)
		{
			if (_ptr != sptr._ptr)
			{
				std::cout << "Shared_Ptr& operator=(const Shared_Ptr& sptr)" << std::endl;
				if (--(*_pcount) == 0)
				{
					delete _ptr;
					delete _pcount;
					_ptr = nullptr;
					_pcount = nullptr;
				}
				_ptr = sptr._ptr;
				_pcount = sptr._pcount;
				++(*_pcount);
			}
			return *this;
		}
		~Shared_Ptr()
		{
			/*if (*_pcount != 0)
			{
				--(*_pcount);
			}
			else*/
			if (--(*_pcount)== 0)
			{
				std::cout << "~Shared_Ptr()" << std::endl;
				delete _ptr;
				delete _pcount;
				_ptr = nullptr;
				_pcount = nullptr;
			}
		}

		T* GetPtr()
		{
			return _ptr;
		}
		const int* GetCount()const
		{
			return _pcount;
		}
		int Use_Count()
		{
			return *_pcount;
		}

		T& operator*()
		{
			return *_ptr;
		}
		T* operator->()
		{
			return _ptr;
		}
	protected:
		T* _ptr;
		int* _pcount;
	};
}